<?php

class Core_Backend_Component_Foundation_Controller_Industry extends Dcmp_Component_Controller {

    public function indexAction($pageindex = 1, $pagesize = 20, $sort = 'id', $dir = 'asc') {
        $this->getHistory()->record();
        $collection = App::getModel('foundation/collection_ritu_industry')
                        ->pagination($pageindex, $pagesize)
                        ->sortBy($sort, $dir);
        if(!$this->getIdentity()->isInRole('resource_manager')){
        	$collection->innerJoin('{t}account_industry', 'z', 'z.industry_id', 'a.id')
        			   ->eq('z.account_id', $this->getIdentity()->getId());
        }                        
        $collection->load();
        $records = $collection->loadCount();
        return $this->viewResult(array('list' => $collection, 'records' => $records));
    }

    public function createAction() {
        $parents = App::getModel('foundation/collection_industry')
                        ->joinRelation()
                        ->resetSelect()
                        ->fields('a.id', 'a.code', 'a.name', 'b.parent_code')
                        ->distinct()
                        ->load();
        return $this->viewResult(array('parents' => $parents));
    }

    public function editAction($id) {
        $model = App::getModel('foundation/industry')->load($id);
        $result = $this->createAction();
        $result->getParents()->removeItemById($model->getId());
        return $result->setModel($model);
    }

    public function importAction(){
        return $this->viewResult();
    }

    public function importPostAction() {
        $file = $this->getRequest()->getFile('file');
        $filename = $file->saveTo('E:\\');
        $datas = fopen($filename, "r");
        while ($data = fgetcsv($datas)) {
            $industry[] = $data;
        }
        $code = NULL;
        for ($i = 0; array_key_exists($i, $industry); $i++) {
            if ($i == 0) {
                $datas = array();
                $code = '110000000000';
                $tempCode = substr($code, 0, 6);
                $code = ++$tempCode . substr($code, 6);
                $datas[] = array('code' => '110001000000', 'name' => $industry[0][0], 'parent_codes' => array('110000000000'));
                $tempCode = substr($code, 0, 9);
                $code = ++$tempCode . substr($code, 9);
                $datas[] = array('code' => $code, 'name' => $industry[0][1], 'parent_codes' => array(--$tempCode . substr($code, 9)));
                $tempCode = $code;
                $datas[] = array('code' => ++$code, 'name' => $industry[0][2], 'parent_codes' => array($tempCode));
            } else {
                if ($industry[$i][0] != $industry[$i - 1][0]) {
                    $tempCode = substr($code, 0, 6);
                    $code = ++$tempCode . substr($code, 9);
                    $datas[] = array('code' => $code, 'name' => $industry[$i][0], 'parent_codes' => array($tempCode . '000000000'));
                    $code = substr($code, 0, 6) . '000000';
                }
                if ($industry[$i][1] != $industry[$i - 1][1]) {
                    $tempCode1 = substr($code, 0, 6);
                    $code = substr($code, 0, 9) . '000';
                    $tempCode = substr($code, 0, 9);
                    $code = ++$tempCode . substr($code, 9);
                    $datas[] = array('code' => $code, 'name' => $industry[$i][1], 'parent_codes' => array($tempCode1 . '000000'));
                }
                $datas[] = array('code' => ++$code, 'name' => $industry[$i][2], 'parent_codes' => array(substr($code, 0, 9) . '000'));
            }
        }

        foreach ($datas as $data) {
            $model = App::getModel('foundation/industry');
            $model->addData($data);
            $model->save();
        }
        $this->getMessager()->addInform($this->__f('share.message.saveSuccess', '{foundation.label.industry_data}', $industry[0][0]));
    }

    public function applyPostAction() {
        return $this->saveModel(FALSE);
    }

    public function savePostAction() {
        return $this->saveModel();
    }

    private function saveModel($return = TRUE) {
        $data = $this->getRequest()->getForm('industry');
        $model = App::getModel('foundation/industry');
        if (isset($data['id']) && !empty($data['id'])) {
            $model->load($data['id']);
        }
        $model->addData($data);
        if (($errors = $model->validate() === TRUE)) {
            $model->save();
            $this->getMessager()->addInform($this->__f('share.message.saveSuccess', '{foundation.label.industry}', $model->getName()));
            return $return ?
                    $this->redirectResult($this->getHistoryUrl('foundation/industry/index')) :
                    $this->redirectResult($this->getUrl(array('id' => $model->getIdValue()), 'edit'));
        } else {
            $this->getMessager()->addError($this->__f('share.message.saveFail', '{foundation.label.industry}', $model->getName()))
                    ->addError($errors);
            return $this->createAction()->setModel($model);
        }
    }

}